package com.sinothk.fc.storeOrderDetail.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailEntity;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailSearchReq;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailVO;
import com.sinothk.fc.storeOrderDetail.mapper.StoreOrderDetailMapper;
import com.sinothk.fc.storeOrderDetail.service.IStoreOrderDetailService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 门店销售记录表
 * @Author: Liangyt
 * @Date: 2022-09-14
 * @Version: V1.0
 */
@Service
public class StoreSaleDetailServiceImpl extends ServiceImpl<StoreOrderDetailMapper, StoreOrderDetailEntity> implements IStoreOrderDetailService {

    @Resource
    private StoreOrderDetailMapper storeOrderRecordMapper;

    @Override
    public IPage<StoreOrderDetailEntity> listForStore(Page<StoreOrderDetailEntity> page, StoreOrderDetailSearchReq searchReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());

//        LambdaQueryWrapper<StoreOrderRecordEntity> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(StoreOrderRecordEntity::getStoreId, loginUser.getStoreId());
//        if (StringUtils.isNotBlank(searchReq.getKeywords())) {
//            wrapper.likeLeft(StoreOrderRecordEntity::getGoodsName, searchReq.getKeywords());
//        }
//        wrapper.orderByDesc(StoreOrderRecordEntity::getCreateTime);
//        return this.page(page, wrapper);
        return storeOrderRecordMapper.listForStore(page, searchReq);
    }

    @Override
    public IPage<StoreOrderDetailVO> listForStoreGroupByOrderId(Page<StoreOrderDetailVO> page, StoreOrderDetailSearchReq searchReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());
        return storeOrderRecordMapper.listForStoreGroupByOrderId(page, searchReq);
    }
}
